﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FlickrNet;
using System.Threading;
using System.IO;
using FlicrkrMe.Upload;

namespace FlickrMe.Upload
{
    public partial class UploadDialog : Form
    {
        delegate void ProgressUpdateCallback(Object obj, UploadProgressEventArgs args);
        delegate void UploadCompleteDelegate();
        delegate void FileUploadStartDelegate(FileInfo info);
        delegate void FileUploadProgressDelegate(long bytesUploaded);

        private long totalBytes;
         private long currentBytes;
        private float uploaded = 0;
        private int noFiles;
        private int noProcessed;

        public UploadDialog()
        {
            InitializeComponent();
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox1.BackColor = Color.Wheat;

            this.Shown += new EventHandler(UploadDialog_Shown);

        }

 
        public void upload_FileUploadProgressEvent(long bytesUploaded)
        {
            FileUploadProgressDelegate d = new FileUploadProgressDelegate(FileUploadProgressEvent);
            this.Invoke(d, new Object[] { bytesUploaded });
        }

        private void FileUploadProgressEvent(long bytesUploaded)
        {
            
            float u = uploaded + bytesUploaded;
            float p = ((float)bytesUploaded / currentBytes) * 100;
            float total = (u / totalBytes) * 100;

            if (p > 100) p = 100;
            if (total > 100) total = 100;

            progressBar1.Value = (int)p;
            progressBar2.Value = (int)total;
        }

 

        private void displayImageDetails(FileInfo fi)
        {
            Bitmap b = new Bitmap(fi.FullName);
            
            pictureBox1.Image = b;
          //  pictureBox1.Update();
            label1.Text = String.Format("copying {0} of {1} files.", noProcessed + 1, noFiles);
            label2.Text = fi.Name;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(this, "Do you really want to cancel?", 
                        "Confirm Cancel", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                this.Close();
            }
           
        }

        public void upload_FileUploadStartEvent(FileInfo info)
        {
            FileUploadStartDelegate d = new FileUploadStartDelegate(FileUploadStart);
            this.Invoke(d, new Object[]{info});
        }

        private void FileUploadStart(FileInfo info) 
        {
            progressBar1.Value = 0;
            currentBytes = info.Length;
            displayImageDetails(info);
        }

        public void upload_FileUploadCompleteEvent()
        {
            noProcessed++;
            uploaded += currentBytes;
        }

        public void upload_UploadStartEvent(int noFiles, long totalBytes)
        {
            this.totalBytes = totalBytes;
            this.noFiles = noFiles;
        }

        public void upload_UploadCompleteEvent()
        {
            UploadCompleteDelegate d = new UploadCompleteDelegate(UploadComplete);
            this.Invoke(d);
        }

        private void UploadComplete()
        {
            this.Close();
        }

        /*
         * Called when this Form is shown to enable it to be brought into view.
         */
        private void UploadDialog_Shown(Object obj, EventArgs args)
        {
            // Ensure that the dialog is visible and not hidden
            // behind another dialog.
            this.TopMost = true;
            this.BringToFront();
            this.Focus();
            this.TopMost = false;
        }
    }

}
